import {
	IParam,
	IPickupViewAbility,
	IPickupViewController,
	IPickupViewControllerParams,
	IPickupViewPanelAbility,
	IPickupViewStore,
} from '@/core/interface';
import { DEViewController } from './de-view-controller';

/**
 * 选择视图控制器
 *
 * @export
 * @class PickupViewController
 * @extends {(DEViewController<T | PickupViewActionType, IPickupViewStore, IPickupViewAbility>)}
 * @template T
 */
export class PickupViewController<
		T,
		S extends IPickupViewStore,
		A extends IPickupViewAbility
	>
	extends DEViewController<T, S, A>
	implements IPickupViewController<S, A>
{
	/**
	 * 处理视图初始化
	 *
	 * @protected
	 * @param {IPickupViewControllerParams<T, A>} params
	 * @memberof ExpViewController
	 */
	protected processViewInit(params: IPickupViewControllerParams<T, A>) {
		super.processViewInit(params);
		Object.assign(this.store, { selections: [] });
	}

	/**
	 * 获取选择视图面板能力
	 *
	 * @protected
	 * @return {*}  {(IPickupViewPanelAbility | undefined)}
	 * @memberof PickupViewController
	 */
	protected getPickupViewPanelAbility(): IPickupViewPanelAbility | undefined {
		const pickupViewPanel = this.model.ctrls.find(
			(ctrl: IParam) => ctrl.controlType === 'PICKUPVIEWPANEL'
		);
		return pickupViewPanel
			? this.getSubAbility<IPickupViewPanelAbility>(pickupViewPanel.name)
			: undefined;
	}

	/**
	 * 处理部件行为
	 *
	 * @param {string} name
	 * @param {string} action
	 * @param {IParam[]} data
	 * @memberof PickupViewController
	 */
	public handleCtrlAction(name: string, action: string, data: IParam[]): void {
		super.handleCtrlAction(name, action, data);
		const pickupViewPanel = this.getPickupViewPanelAbility();
		if (pickupViewPanel && pickupViewPanel.name === name) {
			this.handlePickupViewPanelAction(action, data);
		}
	}

	/**
	 * 处理选择视图面板事件
	 *
	 * @param {string} action
	 * @param {IParam[]} data
	 * @memberof PickupViewController
	 */
	public handlePickupViewPanelAction(action: string, data: IParam[]) {
		switch (action) {
			case 'selectionChange':
				this.handleValueChange(data);
				break;
			case 'dataActive':
				this.handleDataActive(data);
				break;
		}
	}

	/**
	 * 处理值变更
	 *
	 * @protected
	 * @param {IParam[]} data
	 * @memberof PickupViewController
	 */
	protected handleValueChange(data: IParam[]) {
		this.store.selections = data;
	}

	/**
	 * 处理数据激活
	 *
	 * @protected
	 * @param {IParam[]} data
	 * @memberof PickupViewController
	 */
	protected handleDataActive(data: IParam[]) {
		this.emit('viewDataChange', this.store.selections);
		this.emit('viewClose', []);
	}

	/**
	 * 处理确认行为
	 *
	 * @memberof PickupViewController
	 */
	public confirm() {
		this.emit('viewDataChange', this.store.selections);
		this.emit('viewClose', []);
	}

	/**
	 * 处理取消行为
	 *
	 * @memberof PickupViewController
	 */
	public cancel() {
		this.emit('viewClose', []);
	}
}
